package com.ebrothers.isns;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import com.ebrothers.isns.activity.ISNSMain;
import com.ebrothers.isns.common.API;
import com.ebrothers.isns.common.Constants;
import com.ebrothers.isns.util.HttpManager;
import com.ebrothers.isns.util.Utils;

public class Welcome extends Activity {
	private static final String TAG = "Welcome";

	private static final int DIALOG_SETTING_NETWORK = 0;
	private static final int DIALOG_CHANGE_SERVER_IP = 1;

	private TextView mLoadingText;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.isns_splash);
		mLoadingText = (TextView) findViewById(R.id.loading_text);
	}

	@Override
	protected void onResume() {
		if (Constants.LOGD) {
			Log.d(TAG, "****** onResume. ");
		}
		// TODO open this
		validateNetworkAndServer();
		super.onResume();
	}

	private void validateNetworkAndServer() {
		if (Constants.LOGD) {
			Log.d(TAG, "****** validateNetworkAndServer. ");
		}
		mLoadingText.setText(R.string.msg_check_network);
		// check network
		if (Utils.isNetworkAvaliable(getApplicationContext())) {
			mLoadingText.setText(R.string.msg_check_server);
			// ping server
			new Thread() {
				@Override
				public void run() {
					if (HttpManager.tryToAccess(API
							.getServerUrl(getApplicationContext()))) {
						if (Constants.LOGD) {
							Log
									.d(TAG,
											"****** try to access server successfully.");
						}
						doneJob();
					} else {
						runOnUiThread(new Runnable() {
							public void run() {
								showDialog(DIALOG_CHANGE_SERVER_IP);
							}
						});
					}
				}
			}.start();
		} else {
			showDialog(DIALOG_SETTING_NETWORK);
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_SETTING_NETWORK:
			return new AlertDialog.Builder(this).setTitle(
					R.string.title_quick_tip).setMessage(
					R.string.msg_need_network).setCancelable(false)
					.setPositiveButton(R.string.label_settings,
							new OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									Intent intent = new Intent(
											Settings.ACTION_WIRELESS_SETTINGS);
									startActivity(intent);
								}
							}).setNegativeButton(android.R.string.cancel,
							new OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									finish();
								}
							}).show();
		case DIALOG_CHANGE_SERVER_IP:
			final View view = getLayoutInflater().inflate(
					R.layout.isns_alert_dialog_set_server, null);
			final EditText serverIP = (EditText) view
					.findViewById(R.id.server_ip);
			serverIP.setText(API.getServerIP(getApplicationContext()));
			return new AlertDialog.Builder(this).setTitle(
					R.string.title_set_server).setView(view).setCancelable(
					false).setPositiveButton(android.R.string.ok,
					new OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							API.setServerIP(getApplicationContext(), serverIP
									.getText().toString());
							validateNetworkAndServer();
						}
					}).setNegativeButton(android.R.string.cancel,
					new OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							finish();
						}
					}).show();
		default:
			break;
		}
		return super.onCreateDialog(id);
	}

	private void doneJob() {
		Intent intent = new Intent();
		intent.setClass(getApplicationContext(), ISNSMain.class);
		startActivity(intent);
		finish();
	}
}